package org.boulderfoodrescue.webapp.server.viewer;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.boulderfoodrescue.webapp.shared.Place;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;

public class Database {
  public static Place addPlace(Place place){
    place.setSaved((new Date()).getTime());
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity databasePlace;
    if(place.getKey() != null && place.getKey() != 0)
      databasePlace = new Entity("place", place.getKey());
    else
      databasePlace = new Entity("place");
    databasePlace.setProperty("name", place.getName());
    databasePlace.setProperty("addressOne", place.getAddressOne());
    databasePlace.setProperty("city", place.getCity());
    databasePlace.setProperty("state", place.getState());
    databasePlace.setProperty("zip_code", place.getZip_code());
    databasePlace.setProperty("ammount", place.getAmount());
    databasePlace.setProperty("username", place.getUsername());
    databasePlace.setProperty("date_saved", place.getSaved());
    databasePlace.setProperty("date", place.getSaved());
    databasePlace.setProperty("exiration_date", place.getExpiration_date());
    databasePlace.setProperty("latitude", place.getLatitude());
    databasePlace.setProperty("logitude", place.getLongitude());
    databasePlace.setProperty("email", place.getEmail());
    databasePlace.setProperty("phone", place.getPhone());
    databasePlace.setProperty("user_name", place.getContact_name());
    databasePlace.setProperty("notes", place.getNotes());
    Long key = datastore.put(databasePlace).getId();
    place.setKey(key);
    return place;
  }
  public static List<Place> getPlaceForUser(String username){ 
    List<Place> places = new LinkedList<Place>();
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Query query = new Query("place");
    query.addFilter("username", Query.FilterOperator.EQUAL, username);
    List<Entity> dPlaces = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(10000));
    for(Entity dPlace:dPlaces){
      Place place = new Place();
      place.setAddressOne((String) dPlace.getProperty("addressOne"));
      place.setCity((String) dPlace.getProperty("city"));
      place.setSaved((Long) dPlace.getProperty("date_saved"));
      place.setName((String) dPlace.getProperty("name"));
      place.setState((String) dPlace.getProperty("state"));
      place.setZip_code((String) dPlace.getProperty("zip_code"));
      place.setAmount((Long) dPlace.getProperty("ammount"));
      place.setExpiration_date((Long) dPlace.getProperty("exiration_date"));
      place.setUsername((String) dPlace.getProperty("username"));
      place.setLatitude((Double) dPlace.getProperty("latitude"));
      place.setLongitude((Double) dPlace.getProperty("logitude"));
      place.setEmail((String) dPlace.getProperty("email"));
      place.setPhone((String) dPlace.getProperty("phone"));
      place.setContact_name((String) dPlace.getProperty("user_name"));
      place.setNotes((String) dPlace.getProperty("notes"));
      place.setKey(dPlace.getKey().getId());
      places.add(place);
    }
    return places;
  }
  public static List<Place> getAllPlaces(){
    List<Place> places = new LinkedList<Place>();
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Query query = new Query("place");
    List<Entity> dPlaces = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(10000));
    for(Entity dPlace:dPlaces){
      Place place = new Place();
      place.setAddressOne((String) dPlace.getProperty("addressOne"));
      place.setCity((String) dPlace.getProperty("city"));
      place.setSaved((Long) dPlace.getProperty("date_saved"));
      place.setName((String) dPlace.getProperty("name"));
      place.setState((String) dPlace.getProperty("state"));
      place.setZip_code((String) dPlace.getProperty("zip_code"));
      place.setAmount((Long) dPlace.getProperty("ammount"));
      place.setExpiration_date((Long) dPlace.getProperty("exiration_date"));
      place.setUsername((String) dPlace.getProperty("username"));
      place.setLatitude((Double) dPlace.getProperty("latitude"));
      place.setLongitude((Double) dPlace.getProperty("logitude"));
      place.setEmail((String) dPlace.getProperty("email"));
      place.setPhone((String) dPlace.getProperty("phone"));
      place.setContact_name((String) dPlace.getProperty("user_name"));
      place.setNotes((String) dPlace.getProperty("notes"));
      place.setKey(dPlace.getKey().getId());
      places.add(place);
    }
    return places;
  }
  public static String updatePlace(Place place){
    place.setSaved((new Date()).getTime());
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity databasePlace = new Entity("place", place.getKey());
    databasePlace.setProperty("name", place.getName());
    databasePlace.setProperty("addressOne", place.getAddressOne());
    databasePlace.setProperty("city", place.getCity());
    databasePlace.setProperty("state", place.getState());
    databasePlace.setProperty("zip_code", place.getZip_code());
    databasePlace.setProperty("ammount", place.getAmount());
    databasePlace.setProperty("username", place.getUsername());
    databasePlace.setProperty("date_saved", place.getSaved());
    databasePlace.setProperty("date", place.getSaved());
    databasePlace.setProperty("exiration_date", place.getExpiration_date());
    databasePlace.setProperty("latitude", place.getLatitude());
    databasePlace.setProperty("logitude", place.getLongitude());
    databasePlace.setProperty("email", place.getEmail());
    databasePlace.setProperty("phone", place.getPhone());
    databasePlace.setProperty("user_name", place.getContact_name());
    databasePlace.setProperty("notes", place.getNotes());
    datastore.put(databasePlace);
    return null;
  }
  public static void removePlace(Place place){
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity databasePlace = new Entity("place", place.getKey());
    datastore.delete(databasePlace.getKey());
  }
}
